//
//  CountryDetailViewController.m
//  hai游～
//
//  Created by jakf_17 on 15/5/22.
//  Copyright (c) 2015年 jakf_17. All rights reserved.
//

#import "CountryDetailViewController.h"
#import "CityDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <AFNetworking/AFNetworking.h>
#import "MyButtonView.h"
#import "CityImageView.h"

@interface CountryDetailViewController ()<UIScrollViewDelegate>

@end
static CGFloat ImageHeight  = 190;
static CGFloat ImageWidth  = 375;
@implementation CountryDetailViewController


- (void)dealloc
{
    [_dic release];
    [_imageView release];
    [_model release];
    
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    CGRect bounds = self.view.bounds;
    self.scrollView.frame = bounds;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor clearColor];
    [self createMyButtons];
    
    [self.view addSubview:self.scrollView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 25)];
    label.text = @"热门地点:";
    label.textColor = [UIColor colorWithRed:40 / 255.0 green:119 / 255.0 blue:12 / 255.0 alpha:1];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:18];
    [self.scrollView addSubview:label];
    [label release];
    self.scrollView.contentSize = CGSizeMake(375, self.view.frame.size.height + 200);
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *website = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.model.type, self.model.destinationId];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    [manager.requestSerializer setValue:@"BreadTrip/android/5.1.0/zh (android OS4.4.4) lte26007 Map/GoogleMap/v1 (HM 2A,Xiaomi) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    
    
    [manager GET:website parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        [self createPhotoView];
        [self createImageView];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }];

}
#pragma mark -
#pragma mark 城市信息
- (void)createPhotoView
{
    NSArray *array = [_dic objectForKey:@"hottest_sites"];
    for (int i = 0; i < array.count; i++) {
        CityImageView *imageView = [[CityImageView alloc] init];
        imageView.frame = CGRectMake(10 + (([[UIScreen mainScreen] bounds].size.width - 30) / 2 + 10) * (i % 2), 330 + (([[UIScreen mainScreen] bounds].size.width - 30) / 2 + 10) * (i / 2), ([[UIScreen mainScreen] bounds].size.width - 30) / 2, ([[UIScreen mainScreen] bounds].size.width - 30) / 2);
        [_scrollView addSubview:imageView];
        DestinationItemModel *model = [[DestinationItemModel alloc] init];
        NSDictionary *dic = array[i];
        [model setValuesForKeysWithDictionary:dic];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
        
        imageView.layer.cornerRadius = 7;
        imageView.layer.masksToBounds = YES;
        imageView.model = model;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, ([[UIScreen mainScreen] bounds].size.width - 30) / 2 - 20, ([[UIScreen mainScreen] bounds].size.width - 30) / 2 - 10, 15)];
        label.textColor = [UIColor whiteColor];
        label.text = model.name;
        [imageView addSubview:label];
        [label release];
        
    }
//    _scrollView.contentSize = CGSizeMake(375, 330 + (([[UIScreen mainScreen] bounds].size.width - 30) / 2 + 10) * ((array.count + 1) % 2));
}
- (void)tapAction:(UITapGestureRecognizer *)sender
{
    CityImageView *imageView = (CityImageView *)sender.view;
    CityDetailViewController *city = [[CityDetailViewController alloc] init];
    city.model = imageView.model;
    [self.navigationController pushViewController:city animated:YES];
}
- (void)createMyButtons
{
    NSArray *array = @[@"实用须知", @"旅行地点", @"精品游记", @"旅行购票",];
    for (int i = 0; i < 4; i++) {
        MyButtonView *button = [[MyButtonView alloc] initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width / 4) * i, 190, [[UIScreen mainScreen] bounds].size.width / 4, [[UIScreen mainScreen] bounds].size.width / 4)];
        button.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:button];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(butttonTapAction:)];
        [button addGestureRecognizer:tap];
        button.label.text = array[i];
        [button release];
    }
    
}
- (void)butttonTapAction:(UITapGestureRecognizer *)sender
{
    MyButtonView *myView = (MyButtonView *)sender.view;
    
}
#pragma mark - 
#pragma mark -imageview下拉放大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset   = self.scrollView.contentOffset.y;
    
    if (yOffset < 0) {
        
        CGFloat factor = ((ABS(yOffset)+ImageHeight)*ImageWidth)/ImageHeight;
        CGRect f = CGRectMake(-(factor-ImageWidth)/2, 0, factor, ImageHeight+ABS(yOffset));
        self.imageView.frame = f;
    } else {
        CGRect f = self.imageView.frame;
        f.origin.y = -yOffset;
        self.imageView.frame = f;
    }
}
- (void)createImageView
{
    NSDictionary *dic = [_dic objectForKey:@"share_args"];
    NSString *name = [_dic objectForKey:@"name"];
    NSNumber *wishCount = [_dic objectForKey:@"wish_to_go_count"];
    NSNumber *number = [_dic objectForKey:@"visited_count"];
    NSString *like = [NSString stringWithFormat:@"%@ 去过 ／ %@ 喜欢", number, wishCount];
    NSDictionary *subDic = [dic objectForKey:@"default"];
    NSString *str = [subDic objectForKey:@"shr_image"];
    self.imageView = [[CountryDetailImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 190)];
    [self.view addSubview:_imageView];
    [_imageView release];

    _imageView.nameLabel.text = name;
    _imageView.likeLabel.text = like;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
}


@end
